<?php

// $Id: user_activity_dbc.class.php 1571 2009-01-13 13:08:33Z ogrebg $

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('user_activity.class.php');


class User_Activity_DBC extends User_Activity implements I_DB_Storable {
	private $db_table_prefix;

	function __construct($db_table_prefix = '') {
		$this->set_db_table_prefix($db_table_prefix);
	}


	public function set_db_table_prefix($db_table_prefix) {
		$this->db_table_prefix = $db_table_prefix;
	}


	public function get_db_table_prefix() {
		return $this->db_table_prefix;
	}


	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = false;

		$sql = "select user, "
						."type, "
						."ts, "
						."data "
					."from ".$this->get_db_table_prefix()."user_activities where id = $id";

		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();
			$this->set_id($id);
			$this->set_type($rez_obj->TYPE);
			$this->set_user($rez_obj->USER);
			$this->set_ts($rez_obj->TS);
			$this->set_data(unserialize(stripslashesh($rez_obj->DATA)));

			$ret = $id;
		}

		return $ret;
	}



	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id($this->get_db_table_prefix()."user_activities_seq");
		if ($id) {

			$user = $this->get_user();
			$type = $this->get_type();
			$ts = $this->get_ts();
			if (!$ts) {
				$ts = date('Y-m-d H:i:s');
			}
			$data = addslashes(serialize($this->get_data()));

			$sql = "insert into ".$this->get_db_table_prefix()."user_activities"
								."(id, "
								."user, "
								."type, "
								."ts, "
								."data "
								.") "
							."values ("
								."$id, "
								."$user, "
								."$type, "
								."'$ts', "
								."'$data' "
								.")";
			$dbc->execute($sql);

			$ret = $id;
		} else {
			throw new TE_Exception("ID not generated - ".$this->get_db_table_prefix()."user_activities_seq");
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$cookie = $this->get_cookie();
		$user = $this->get_user();

		$user = $this->get_user();
		$type = $this->get_type();
		$ts = $this->get_ts();
		$data = addslashes(serialize($this->get_data()));

		$sql = "update ".$this->get_db_table_prefix()."user_activities set "
							."user = $user, "
							."type = $type, "
							."ts = '$ts', "
							."data = '$data' "
					."where id = ".$this->get_id();

		$dbc->execute($sql);

		return $this->get_id();
	}


	public static function delete(DB_Connection $dbc, $id, $prefix = '') {
		tangra_if_not_int_throw_e($id);
		$sql = "delete from ".$prefix."user_activities where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid($user_id, $prefix = '') {
		tangra_if_not_int_throw_e($user_id);

		$sql = "select uat.description, ua.ts, ua.data from {$prefix}user_activities ua, {$prefix}user_activity_types uat where ua.user = $user_id and ua.type = uat.id order by ua.id desc ";

		return $sql;
	}


	public static function get_sql_count_for_grid($user_id, $prefix = '') {
		tangra_if_not_int_throw_e($user_id);

		$sql = "select count(id) as total_rows from {$prefix}user_activities where user = $user_id";

		return $sql;
	}


	public static function delete_activities_for_user(DB_Connection $dbc, $user_id, $prefix = '') {
		tangra_if_not_int_throw_e($user_id);
		$sql = "delete from {$prefix}user_activities where user = $user_id";
		$dbc->execute($sql);
	}
}
